﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Meta.Quote.Data;
using AAA.Base.Util;
using AAA.Meta.Quote;

namespace AAA.Quote
{
    public class KBarHelper
    {
        public static List<DateTime> GenerateBarTime(string strStartTime, string strEndTime, DateTime dtToday, BarCompression barCompression)
        {
            List<DateTime> lstReturn = new List<DateTime>();
            DateTime dtCurrentTime;
            DateTime dtEndTime;

            switch (barCompression.DataCompression)
            {
                case BarCompressionEnum.Daily:
                    lstReturn.Add(DateTimeHelper.Parse(dtToday.ToString("yyyy/MM/dd 00:00:00")));                        
                    break;
                case BarCompressionEnum.Min:
                    dtCurrentTime = DateTimeHelper.Parse(dtToday.ToString("yyyy/MM/dd ") + strStartTime);
                    dtEndTime = DateTimeHelper.Parse(dtToday.ToString("yyyy/MM/dd ") + strEndTime);

                    lstReturn.Add(dtCurrentTime);
                    while (dtCurrentTime.CompareTo(dtEndTime) < 0)
                    {
                        dtCurrentTime = dtCurrentTime.AddMinutes(barCompression.Interval);
                        lstReturn.Add(dtCurrentTime.CompareTo(dtEndTime) < 0 ? dtCurrentTime : dtEndTime);
                    }

                    break;
            }

            return lstReturn;
        }

        public static DateTime KBarTime(string strStartTime, DateTime dtCurrent, BarCompression barCompression)
        {

            DateTime dtStartTime = DateTimeHelper.Parse(dtCurrent.ToString("yyyy/MM/dd") + " " + strStartTime);
            if (dtCurrent.Ticks <= dtStartTime.Ticks)
                dtStartTime = dtStartTime.AddSeconds(1);

            switch (barCompression.DataCompression)
            {
                case BarCompressionEnum.Daily:
                    return DateTimeHelper.Parse(dtCurrent.ToString("yyyy/MM/dd 00:00:00"));
                    
                case BarCompressionEnum.Min:
                    TimeSpan timeInterval = new TimeSpan(dtCurrent.Ticks - dtStartTime.Ticks);

                    return dtStartTime.AddMinutes(Math.Ceiling((timeInterval.Seconds / (60.0 * barCompression.Interval))) * barCompression.Interval);
            }

            return DateTime.MinValue;
        }
    }
}
